package com.service;

import Dbcon.DBcon;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * Created by ztc on 16-3-21.下午3:58
 */
public class QRService {
    //qrcodesigin(uuid varchar(130),teacherID varchar(10),stuID varchar(10),
    // stuName varchar(10),ip varchar(36),localName varchar(50));

    public boolean isReflash(){
        System.out.println("isReflash");
        String sql="select stuID from qrcodesigin where stuName is null;";
        Connection con=DBcon.getConnction();
        try {
            PreparedStatement pr=con.prepareStatement(sql);
            ResultSet re=pr.executeQuery();
            boolean result=re.next();
            re.close();
            pr.close();
            con.close();
            if(result)
                return false;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return true;
    }
    public boolean isUuid(String uuid){
        System.out.println("isUuid");
        String sql="select*from qrcodesigin where uuid=? and stuID is null;";
        Connection con=DBcon.getConnction();
        try {
            PreparedStatement pr=con.prepareStatement(sql);
            pr.setString(1,uuid);
            ResultSet re=pr.executeQuery();
            boolean result=re.next();
            re.close();
            pr.close();
            con.close();
            if(result)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean signIn(String uuid,String stuID,String stuName,String ip,String localName){
        System.out.println("signIn");
        String sql="update qrcodesigin set stuID=?,stuName=?,ip=?,localName=? where uuid=?;";
        Connection con= DBcon.getConnction();
        try {
            PreparedStatement pr=con.prepareStatement(sql);
            pr.setString(1,stuID);
            pr.setString(2,stuName);
            pr.setString(3,ip);
            pr.setString(4,localName);
            pr.setString(5,uuid);
            int i=pr.executeUpdate();
            pr.close();
            con.close();
            if(i==1)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
    public boolean insertInto(String uuid,String teacherID){
        System.out.println("insertInto");
        String sql="insert into qrcodesigin(uuid,teacherID) values(?,?);";
        Connection con= DBcon.getConnction();
        PreparedStatement pr;
        try {
            pr=con.prepareStatement(sql);
            pr.setString(1,uuid);
            pr.setString(2,teacherID);
            int i=pr.executeUpdate();
            pr.close();
            con.close();
            if(i==1)
                return true;
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;
    }
}
